#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->statusbar->addWidget(&labFileName);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actWindowVisiable_toggled(bool arg1)
{
    ui->dockWidget->setVisible(arg1);
}

void MainWindow::on_actWindowFloating_toggled(bool arg1)
{
    ui->dockWidget->setFloating(arg1);
}

//void MainWindow::on_dockWidget_topLevelChanged(bool topLevel)
//{
//    ui->actWindowFloating->setChecked(topLevel);
//}

//void MainWindow::on_dockWidget_visibilityChanged(bool visible)
//{
//    ui->actWindowVisiable->setVisible(visible);
//}

void MainWindow::on_actAddFolder_triggered()
{
    QString fullNamePath = QFileDialog::getExistingDirectory();
    if(fullNamePath.isEmpty()) return;

    QString dir = fullNamePath.right(fullNamePath.length() - fullNamePath.lastIndexOf("/") - 1);
    if(!ui->treeWidget->currentIndex().isValid())
        return;

    QTreeWidgetItem *parentItem = ui->treeWidget->currentItem();
    QTreeWidgetItem *item = new QTreeWidgetItem(TreeItemType::itGroupItem);
    QIcon icon(":/icons/dir.svg");
    item->setIcon(TreeColumnNum::colItem, icon);
    item->setText(TreeColumnNum::colItem, dir);
    item->setText(TreeColumnNum::colItemType, "type:group");
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsAutoTristate | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
    item->setCheckState(TreeColumnNum::colItem, Qt::Checked);
    parentItem->addChild(item);
}

void MainWindow::on_actAddFiles_triggered()
{
    QStringList files = QFileDialog::getOpenFileNames(this, "选择一个或多个文件", "", "Images(*.png)");

    QTreeWidgetItem *parentItem;
    QTreeWidgetItem *item = ui->treeWidget->currentItem();
    if(item->type() == TreeItemType::itImageItem)
        parentItem = item->parent();
    else
        parentItem = item;

    foreach(auto file, files)
    {
        item = new QTreeWidgetItem(TreeItemType::itImageItem);
        QString str = file.right(file.length() - file.lastIndexOf("/") - 1);
        QIcon icon(":/icons/picture.svg");
        item->setIcon(TreeColumnNum::colItem, icon);
        item->setText(TreeColumnNum::colItem, str);
        item->setText(TreeColumnNum::colItemType, "imageFile");
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsAutoTristate | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
        item->setCheckState(TreeColumnNum::colItem, Qt::Checked);
        item->setData(TreeColumnNum::colItem, Qt::UserRole, file);
        parentItem->addChild(item);
    }
}

void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    Q_UNUSED(previous);

   if(!current) return;
   int type = current->type();
   switch (type) {
       case itTopItem:
           ui->actAddFiles->setEnabled(true);
           ui->actAddFolder->setEnabled(true);
           break;
       case itGroupItem:
           ui->actAddFiles->setEnabled(true);
           ui->actAddFolder->setEnabled(true);
          break;
       case itImageItem:
          ui->actAddFiles->setEnabled(true);
          ui->actAddFolder->setEnabled(false);
          labFileName.setText(current->data(colItem, Qt::UserRole).toString());
          curPixmap.load(current->data(colItem, Qt::UserRole).toString());
          on_actFitWidth_triggered();
          break;
       default:
           break;
   }
}

void MainWindow::on_actFitWidth_triggered()
{
    int scrollAreaHeight = ui->scrollArea->height();
    int realImgHeight = curPixmap.height();
    pixRatio = (float)scrollAreaHeight / realImgHeight;
    QPixmap pix = curPixmap.scaledToHeight(scrollAreaHeight - 30);
    ui->label->setPixmap(pix);
}

void MainWindow::on_actFitHeight_triggered()
{
    int scrollAreaWidth = ui->scrollArea->width();
    QPixmap pix = curPixmap.scaledToHeight(scrollAreaWidth - 30);
    ui->label->setPixmap(pix);
}

void MainWindow::on_actScanItem_triggered()
{
    for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    {
        QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);

    }
}

void MainWindow::on_actDeleteItem_triggered()
{
    QTreeWidgetItem *item = ui->treeWidget->currentItem();
    QTreeWidgetItem *parentItem = item->parent();
    parentItem->removeChild(item);
    delete item;
}

void MainWindow::scaleImage(float ratio)
{
    int w = ratio * curPixmap.width();
    int h = ratio * curPixmap.height();
    QPixmap pix = curPixmap.scaled(w, h);
    ui->label->setPixmap(pix);
}

void MainWindow::on_actZooOut_triggered()
{
    pixRatio *=1.2;
    scaleImage(pixRatio);
}

void MainWindow::on_actZoomIn_triggered()
{
    pixRatio /=1.2;
    scaleImage(pixRatio);
}

void MainWindow::on_actRealSize_triggered()
{
    pixRatio = 1;
    scaleImage(pixRatio);
}
